import time

from common.base import Base

home_page_url = "http://ecshop.itsoso.cn/"


class GoodsList(Base):
    """制作定位器"""
    goods_loc = ("css selector", "div.goods-title>a")  # 全部商品
    phone_loc = ("link text", "手机")  # 手机
    work_loc = ("link text", "办公")  # 办公
    household_appliances_loc = ("link text", "家用电器")  # 家用电器
    computer_loc = ("link text", "电脑")  # 电脑
    clothing_loc = ("link text", "男装/女装")  # 男装/女装
    digital_fashion_loc = ("link text", "数码时尚")  # 数码时尚
    maternal_loc = ("link text", "母婴/玩具")  # 母婴/玩具
    food_wine_loc = ("link text", "食品/酒类")  # 食品/酒类
    exercise_outdoors_loc = ("link text", "运动/户外")  # 运动/户外

    """创建操作类"""

    def click_phone(self):
        """点击手机"""
        self.click(self.phone_loc)
        time.sleep(1)

    def click_work(self):
        """点击办公"""
        self.click(self.work_loc)
        time.sleep(1)

    def click_house_appliances(self):
        """点击家用电器"""
        self.click(self.household_appliances_loc)
        time.sleep(1)

    def click_computer(self):
        """点击电脑"""
        self.click(self.computer_loc)
        time.sleep(1)

    def click_clothing(self):
        """点击男装/女装"""
        self.click(self.clothing_loc)
        time.sleep(1)

    def click_digital_fashion(self):
        """点击点击数码时尚"""
        self.click(self.digital_fashion_loc)
        time.sleep(1)

    def click_maternal(self):
        """点击母婴/玩具"""
        self.click(self.maternal_loc)
        time.sleep(1)

    def click_food_wine(self):
        """点击食品/酒类"""
        self.click(self.food_wine_loc)
        time.sleep(1)

    def click_exercise_outdoors(self):
        """点击运动/户外"""
        self.click(self.exercise_outdoors_loc)
        time.sleep(1)

    def get_goods_titles(self):
        """获取所有商品标题"""
        goods_elements = self.find_elements(self.goods_loc)
        titles = []
        for i in goods_elements:
            title = i.get_attribute("title")
            titles.append(title)
        return titles

    def click_goods_all(self):
        """点击全部商品"""
        titles = self.get_goods_titles()
        for title in titles:
            good_loc = ("css selector", f"a[title='{title}']")
            self.click(good_loc)
            time.sleep(1)
            self.back()
            time.sleep(2)
